package com.example.weiwing.home.dynamic;


import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.example.weiwing.R;
import com.example.weiwing.config.WeiWingConfig;
import com.example.weiwing.entity.Article;
import com.example.weiwing.util.SpUtils;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;

public class DynamicView extends AppCompatActivity {

    private Handler mHandler;

    private Article article;
    private WebView webView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_dynamic);

        Toolbar myToolbar = findViewById(R.id.dynamicViewToolBar);
        myToolbar.setTitle("");
        setSupportActionBar(myToolbar);
        // 启用返回按钮
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        webView = findViewById(R.id.DynamicwebView);

        // 获取 WebView 的设置
        WebSettings webSettings = webView.getSettings();

        Intent intent = getIntent();
        String articleId = intent.getStringExtra("articleId");

//        String articleId = "1732451224032952322";

        mHandler = new Handler() {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);

                if (msg.what == 1) {
                    String customHtml = "<html><body><h1>" + article.getTitle() + "</h1><p>" + article.getContent() + "</p><img src=\"" + WeiWingConfig.getUrl() + article.getImgUrl() + "\" style=\"width:100%\"></body></html>";
                    webView.loadData(customHtml, "text/html", "UTF-8");
                }

            }
        };

        ProgressDialog progressDialog = ProgressDialog.show(DynamicView.this, "动态详情", "加载中...");
        new Thread(() -> HttpRequest.build(DynamicView.this, WeiWingConfig.getUrl() + "/article/" + articleId)
                .setAsync(false)
                .addHeaders("token", SpUtils.getString(DynamicView.this, "token"))
                .setResponseListener(new ResponseListener() {
                    @Override
                    public void onResponse(String response, Exception error) {
                        progressDialog.dismiss();
                        if (error == null) {
                            JSONObject jsonObject = JSON.parseObject(response);
                            if (jsonObject.getInteger("code") == 200) {
                                article = jsonObject.getObject("data", new TypeReference<Article>() {
                                });
                                mHandler.obtainMessage(1).sendToTarget();
                            } else {
                                Toast.makeText(DynamicView.this, "未知错误", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(DynamicView.this, "请求失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                }).doGet()
        ).start();


        // 启用 JavaScript 支持
//        String customHtml = "<html><body><h1>" + article.getTitle() + "</h1><p>" + article.getContent() + "</p><img src=\"" + WeiWingConfig.getUrl() + article.getImgUrl() + "\"></body></html>";
        String customHtml = "";
        webView.loadData(customHtml, "text/html", "UTF-8");
    }


    // 处理返回按钮点击事件
    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.user_dynamic_view_menu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // 处理点击事件
        if (item.getItemId() == R.id.dynamicMenuShare) {
            // 用户点击了分享
            return true;
        } else if (item.getItemId() == R.id.dynamicMenuModify) {
            // 用户点击了修改
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
